package edu.bw.service.impl;

import edu.bw.dao.RoleDao;
import edu.bw.dao.impl.RoleDaoImpl;
import edu.bw.dto.InsertRoleForm;
import edu.bw.dto.SelectRoleByConditionForm;
import edu.bw.dto.UpdateRoleForm;
import edu.bw.pojo.Role;
import edu.bw.service.RoleService;
import edu.bw.utils.PageUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


//service层要写业务逻辑  也就是分页要在这里写
public class RoleServiceImpl implements RoleService {
    RoleDao roleDao =new RoleDaoImpl();
    @Override
    public PageUtils selectRoleByCondition(SelectRoleByConditionForm selectRoleByConditionForm) {
        List<Map<String,Object>> list= roleDao.selectRoleByCondition(selectRoleByConditionForm);

        Long count=roleDao.selectRoleByConditionCount(selectRoleByConditionForm);

        int beginIndex=(selectRoleByConditionForm.getPage()-1)*selectRoleByConditionForm.getLength();
        return new PageUtils(list,count,beginIndex,selectRoleByConditionForm.getLength());
    }

    @Override
    public Integer insert(InsertRoleForm insertRoleForm) {
        Role role = new Role();
        role.setRoleName(insertRoleForm.getRoleName());
        role.setPermissions(Arrays.toString(insertRoleForm.getPermissions()));
        role.setDesc(insertRoleForm.getDesc());

        return roleDao.insert(role);
    }

    @Override
    public List<Map<String, Object>> selectById(Integer id) {
        return roleDao.selectById(id);
    }

    @Override
    public Integer update(UpdateRoleForm updateRoleForm) {
        return roleDao.update(updateRoleForm);
    }

    @Override
    public Integer deleteByIds(List<Integer> ids) {
        Integer rows=0;
        if (roleDao.selectCanDelete(ids)){
            rows=roleDao.deleteRoleByIds(ids);
        }
        return rows;
    }

    @Override
    public List<Map<String, Object>> selectAllRole() {
        return roleDao.selectAllRole();
    }
}
